package com.zyf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyf.common.result.Result;
import com.zyf.common.util.AuthContextHolder;
import com.zyf.enums.OrderStatusEnum;
import com.zyf.model.order.OrderInfo;
import com.zyf.service.OrderInfoService;
import com.zyf.vo.order.OrderQueryVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/** 订单操作，控制层类
 * @author zyf
 * @date 2021年05月31日 14:40
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation(value = "生成订单Api")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleId", value = "排班id"),
            @ApiImplicitParam(name = "patientId", value = "就诊人id")
    })
    public Result submitOrder(@PathVariable String scheduleId,@PathVariable Long patientId){
        Long orderId = orderInfoService.submitOrder(scheduleId,patientId);
        return Result.ok(orderId);
    }

    @ApiOperation(value = "根据订单id查询订单的详细信息")
    @GetMapping("/auth/getOrders/{orderId}")
    @ApiImplicitParam(name = "orderId", value = "订单id")
    public Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo = orderInfoService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation(value = "获取订单列表")
    @GetMapping("/auth/getOrderList/{page}/{limit}")
    public Result getOrderList(@PathVariable Long page, @PathVariable Long limit, OrderQueryVo queryVo, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        queryVo.setUserId(userId);
        Page<OrderInfo> pageParam = new Page<>(page, limit);

        IPage<OrderInfo> iPage = orderInfoService.selectPage(pageParam,queryVo);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("/auth/getStatusList")
    public Result getStatusList(){
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消订单")
    @GetMapping("/auth/cancelOrder/{orderId}")
    @ApiImplicitParam(name = "orderId", value = "订单id")
    public Result cancelOrder(@PathVariable String orderId){
        Boolean flag = orderInfoService.cancelOrder(orderId);
        return Result.ok(flag);
    }

}
